/**
 * 
 */
package utility.io;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.nio.file.Paths;

import auth.User;


/**
 * @author Louis
 *
 */
public abstract class HWCharacterIO {
	
	private Path dbPath;
	
	HWCharacterIO(String path){
		this.setPath(Paths.get(path+File.separator+getFormat()));
	}
	
	public abstract void loadDataBase() throws IOException;
	public abstract void saveDataBase() throws IOException;
	public abstract void saveCollections(User name) throws  IOException;
	public abstract void loadCollections(User name) throws IOException;
	public abstract String getFormat();
	
	public Path getPath() {
		return dbPath;
	}

	public void setPath(Path path) {
		this.dbPath = path;
	}
	
	protected String idToName(String key){
		String r = "";
		try {
			byte[] bytes = key.getBytes("UTF-8"); 
			for (int b = 0; b<bytes.length; b++) {
			   r += Integer.toHexString(bytes[b])+((b==bytes.length-1)?"":";");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return r;
	}
	
	protected String nameToId(String name){
		String[] bytes = name.split(";");
		byte[] id = new byte[bytes.length];
		try {
			for (int b = 0; b<bytes.length;b++) {
			   id[b] = (byte)Integer.parseInt(bytes[b]);
			}
			return new String(id,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
